<?php
	class Post extends CI_Controller {
		
		/*==========================User Post Actions==============================*/
		public function authuser(){
			
			$this->form_validation->set_rules('username', '', 'required');
			$this->form_validation->set_rules('password', '', 'required');
			$this->form_validation->set_error_delimiters('<div class="error">','</div>');
			$usertype = $this->input->post('usertype');
			$data['title'] = ucfirst($usertype).' Log In';
			$data['usertype'] = $usertype;
			if($this->form_validation->run() == false){
							
				$this->load->view('vHead', $data);
				$this->load->view('page-blocks/vHeader');
				$this->load->view('page-blocks/vLogin', $data);
				$this->load->view('page-blocks/vFooter');
				$this->load->view('vFoot');
				
			}
			else {
				$this->load->model('M_user');
				$v = (object) $this->input->post();
				if(!$this->M_user->getUser($v)){
					$data['validation_result'] = 'Invalid Username/Password';
					$this->load->view('vHead', $data);
					$this->load->view('page-blocks/vHeader');
					$this->load->view('page-blocks/vLogin', $data);
					$this->load->view('page-blocks/vFooter');
					$this->load->view('vFoot');
				}
				else {
					redirect($this->session->userdata('usertype'));
				}
			}
		}
		
		public function logout(){
			$this->session->sess_destroy();
			redirect('home');	
		}
		
		public function add_user(){
			$this->form_validation->set_rules('username', '', 'required');
			$this->form_validation->set_rules('password', '', 'required');
			$this->form_validation->set_rules('usertype', '', 'required|alpha');
			$this->form_validation->set_message('alpha','The usertype field is required');
			
			if($this->form_validation->run() == false){
				$this->set_message_redirect('error', validation_errors(), 'admin/add_user');						
			}
			else {
				$this->load->model('M_user');
				$v = (object) $this->input->post();

			
				
				if($this->M_user->add_user($v)){
					$this->set_message_redirect('success', 'User added successfully', 'admin/add_user');
				}
				


			}
		}
		/*==========================End User Post Actions==============================*/
		
		/*==========================Change Password Post Actions==============================*/
		function change_password(){
			$this->form_validation->set_rules('oldpassword', '', 'required');
			$this->form_validation->set_rules('newpassword', '', 'required');
			$this->form_validation->set_rules('retype', '', 'required');
			

			
			if($this->form_validation->run() == false){
				$this->set_message_redirect('error', validation_errors(), 'admin/change_password');			
			}
			else {
				if($this->input->post('retype') != $this->input->post('newpassword'))
					$this->set_message_redirect('error', 'Password and Re-typed password does not match', 'admin/change_password');
				else{
					$this->load->model('M_user');
					
					if($this->M_user->validate_password($this->M_user->get_user_password($this->session->userdata('userid')), $this->input->post('oldpassword'))){
						if($this->M_user->change_password($this->input->post('newpassword'), $this->session->userdata('userid')))
							$this->set_message_redirect('success', 'Password successfully changed!', 'admin/change_password');
						else
							$this->set_message_redirect('error', 'Please try again', 'admin/change_passsword');
					}
					else
						$this->set_message_redirect('error', 'Your old password does not match the system entry', 'admin/change_password');
					
				}
					
					
									
								
				
			}
		}
		/*==========================Change Password Post Actions==============================*/
		
		/*==========================School Post Actions==============================*/
		public function update_school(){
			$this->load->model('M_school');
			
			if($this->M_school->update_school($this->input->post('schoolname'), $this->input->post('schooladdress'), $this->input->post('schoolemail'), $this->input->post('schooltelephone'))){
				$this->set_message_redirect('success', 'School updated successfully!', 'admin/school_setting');
			}
			else{
				$this->set_message_redirect('error', 'Please try again', 'admin/add_year_level');
			}
		}
		/*==========================End School Post Actions==============================*/
		
		/*==========================Year Level Post Actions==============================*/
		public function add_year_level(){
			$this->load->model('M_course');
			$this->form_validation->set_rules('level_name','Level Name','required');
			$this->form_validation->set_rules('year_level_fees','Year Level Fee','required');
			if($this->form_validation->run() !== FALSE){
				$input['level_name'] = $this->input->post('level_name',TRUE);
				if($this->M_course->add_year_level($input) !== FALSE)
					$id = $this->M_course->get_last_insert_id();
					foreach($this->input->post('year_level_fees',TRUE) as $fees){
							$ylf['fee_cat_id'] = $fees;
							$ylf['year_level_id'] = $id;
							$this->M_course->insert_year_level_fees($ylf);
					}
				$this->set_message_redirect('success', 'Level added successfully!', 'admin/add_year_level');
			}
			else {
				$this->set_message_redirect('error', validation_errors(), 'admin/add_year_level');
			}
		}
		
		public function delete_year_level($level_id){
			$this->load->model('M_course');

			if($this->M_course->delete_year_level($level_id))
				$this->set_message_redirect('success', 'Level deleted successfully!', 'admin/level');
			else
				$this->set_message_redirect('error', 'Please try again', 'admin/level');
				
		}
		
		public function edit_year_level()
		{
			$this->load->model('M_course');
			$id=$this->input->post('level_id',TRUE);
			$name=$this->input->post('level_name',TRUE);
			
			$this->form_validation->set_rules('level_name','Level Name','required');
			if($this->form_validation->run() == FALSE)
			{
				$this->set_message_redirect('error',validation_errors(), 'admin/edit_year_level/'.$id);
			}else
			{
				if($this->input->post('fee') !== '' AND $this->input->post('fee') !== FALSE AND $this->input->post('fee') !== NULL)
				{
						if($this->M_course->edit_year_level($id,$name))
						{
							if($this->M_course->free_year_level_fees($id))
							{
								foreach($this->input->post('fee') as $fee)
								{
									$input['fee_cat_id'] = $fee;
									$input['year_level_id'] = $id;
									$input['updated']	 =NOW;
									$this->M_course->edit_year_level_fees($input);
								}
								if($this->M_course->get_insert_status() == TRUE)
								{
									$this->set_message_redirect('success','Successfully processed your request', 'admin/edit_year_level/'.$id);
								}else
								{
									$this->set_message_redirect('error','An error occured while processing request', 'admin/edit_year_level/'.$id);
								}
							}else
							{
								$this->set_message_redirect('error','An error occured while processing request', 'admin/edit_year_level/'.$id);
							}
						}else
						{
							$this->set_message_redirect('error','An error occured while processing request', 'admin/edit_year_level/'.$id);
						}
				}else
				{
					if($this->M_course->edit_year_level($id,$name) == TRUE){
						$this->set_message_redirect('success','Successfully process request', 'admin/edit_year_level/'.$id);
					}else{
						$this->set_message_redirect('error','An error occured while processing request', 'admin/edit_year_level/'.$id);
					}
				}
			}
		}
		/*======================End Year Level Post Actions==================================*/
		
		/*==========================Semester Post Actions==============================*/
		public function add_semester(){
			$this->load->model('M_course');
			
			if($this->M_course->add_semester($this->input->post('semestername'))){
				$this->set_message_redirect('success', 'Semester added successfully!', 'admin/add_semester');
			}
			else {
				$this->set_message_redirect('error', 'Please try again', 'admin/add_semester');
			}
		}
		
		public function delete_semester($semester_id){
			$this->load->model('M_course');

			if($this->M_course->delete_semester($semester_id))
				$this->set_message_redirect('success', 'Semester deleted successfully!', 'admin/semester');
			else
				$this->set_message_redirect('error', 'Please try again', 'admin/semester');
				
		}
		
		public function edit_semester(){
			$this->load->model('M_course');
			
			if($this->M_course->edit_semester($this->input->post('semester_id'), $this->input->post('semestername')))
				$this->set_message_redirect('success', 'Level edited successfully!', 'admin/semester');
			else
				$this->set_message_redirect('error', 'Please try again', 'admin/edit_semester');
		}
		/*======================End Semester Post Actions==================================*/
		
		/*==========================Subject Post Actions==============================*/
		public function add_subject(){
			$this->load->model('M_course');
			
			if($this->M_course->add_subject($this->input->post('subjectname'))){
				$this->set_message_redirect('success', 'Subject added successfully!', 'admin/add_subject');
			}
			else {
				$this->set_message_redirect('error', 'Please try again', 'admin/add_subject');
			}
		}
		
		public function delete_subject($subject_id){
			$this->load->model('M_course');

			if($this->M_course->delete_subject($subject_id))
				$this->set_message_redirect('success', 'Subject deleted successfully!', 'admin/subject');
			else
				$this->set_message_redirect('error', 'Please try again', 'admin/subject');
				
		}
		
		public function edit_subject(){
			$this->load->model('M_course');
			
			if($this->Course->edit_subject($this->input->post('subject_id'), $this->input->post('subjectname')))
				$this->set_message_redirect('success', 'Subject edited successfully!', 'admin/subject');
			else
				$this->set_message_redirect('error', 'Please try again', 'admin/edit_subject');
		}
		/*======================End Subject Post Actions==================================*/
		
		/*==========================Fee Post Actions==============================*/
		public function add_fee(){
		
			$this->load->model('M_finance');
			$this->form_validation->set_rules('fee_name','','required');
			$this->form_validation->set_rules('fee_desc','','required');
			
			$input['fee_name'] = $this->input->post('fee_name');
			$input['fee_desc'] = $this->input->post('fee_desc');
			$input['fee_created_at'] = NOW;
			$input['fee_updated_at'] = NOW;
		
		
			if($this->form_validation->run() == FALSE){
				$this->set_message_redirect('error', validation_errors(),'admin/add_fee');
			}else{
				if($this->M_finance->add_fee($input) == FALSE){
					$this->set_message_redirect('error','An error was encountered while trying to add data','admin/add_fee');
				}else{
					$this->set_message_redirect('success', 'Fee added successfully!', 'admin/fee_creator');
				}			
			}
		}
		
		public function edit_fee_category(){
			$this->load->model('M_finance');
			$cat_id = $this->input->post('fee_cat_id');
			$cat_name = $this->input->post('fee_cat_name');
			$cat_desc = $this->input->post('fee_cat_desc');
			if($this->input->post('fee') !== '' AND $this->input->post('fee') !== NULL AND $this->input->post('fee') !== FALSE ){
				if($this->M_finance->edit_fee_cat_prof($cat_id,$cat_name,$cat_desc) == TRUE){
					$this->M_finance->free_fee_cat($cat_id);
					foreach($this->input->post('fee') as $fee_id){
							$test['fee_id']= $fee_id;
							$this->M_finance->edit_fee_cat($test,$cat_id);
					}
				}else{
					$this->set_message_redirect('error', 'An error occured while trying to process request, please try again', 'admin/edit_fee_cat/'.$cat_id);
				}
				$this->set_message_redirect('success', 'Category Updated', 'admin/edit_fee_cat/'.$cat_id);
			}else{
				$this->M_finance->free_fee_cat($cat_id);
				$this->set_message_redirect('success', 'Category Updated', 'admin/edit_fee_cat/'.$cat_id);
				
			}
		}
		
		public function delete_fee_cat($id){
			$this->load->model('M_finance');
			if($this->M_finance->free_fee_cat($id,TRUE) == TRUE){
				$this->set_message_redirect('success', 'Category Deleted', 'admin/fee_category_creator');
			}else{
				$this->set_message_redirect('error', 'An error occured while trying to process request, please try again', 'admin/fee_category_creator');
			}
		}
		
		
		public function delete_fee($fee_id){
			$this->load->model('M_finance');

			if($this->M_finance->delete_fee($fee_id))
				$this->set_message_redirect('success', 'Fee deleted successfully!', 'admin/fee_creator');
			else
				$this->set_message_redirect('error', 'Please try again', 'admin/fee_creator');
				
		}
		
		public function edit_fee(){
			$this->load->model('M_finance');
			$id = $this->input->post('fee_id');
			$input['fee_name'] = $this->input->post('fee_name',TRUE);
			$input['fee_desc'] = $this->input->post('fee_desc',TRUE);
			$input['fee_rate'] = $this->input->post('fee_rate',TRUE);
			$input['fee_updated_at'] = NOW;
			if($this->M_finance->edit_fee($input,$id))
				$this->set_message_redirect('success', 'Subject edited successfully!', 'admin/fee_creator');
			else
				$this->set_message_redirect('error', 'Please try again', 'admin/fee_creator');
		}
		
		public function add_fee_category(){
			$this->load->model('M_finance');
			
			$this->form_validation->set_rules('fee_cat_name','Category Name','required');
			$this->form_validation->set_rules('fee_cat_desc','Category Description','required');
			$input['fee_cat_name'] = $this->input->post('fee_cat_name');
			$input['fee_cat_desc'] = $this->input->post('fee_cat_desc');
			$input['fee_cat_created_at'] = NOW;
			if($this->form_validation->run() == FALSE){
					$this->set_message_redirect('error', validation_errors(), 'admin/add_fee_category');
			}else{
				if($this->M_finance->add_fee_category($input) == FALSE)
					$this->set_message_redirect('error', 'Error Encountered while processing request', 'admin/add_fee_category');
				else
					$this->set_message_redirect('success', 'A Category has been added', 'admin/fee_category_creator');
			}
			
			
		}
		/*======================End Fee Post Actions==================================*/
		
		/*======================Select Enrollment Type Post Actions==================================*/
		public function select_enroll(){
			$this->form_validation->set_rules('enrolle_type', '', 'required');
			
			if($this->form_validation->run() == FALSE){
				$this->set_message_redirect('error', 'Please select enrollment type', 'enroll');
			}
			else {
				redirect('enroll/'.$this->input->post('enrolle_type'));	
			}
		}
		/*======================End Select Enrollment Type Post Actions==================================*/
		
		/*======================New Enrollee Type Post Actions==================================*/
		public function new_enrollee(){
			$this->form_validation->set_rules('lName', '', 'required');
			$this->form_validation->set_rules('fName', '', 'required');

			if($this->form_validation->run() == FALSE){
				$this->set_message_redirect('error', 'Please select enrollment type', 'enroll/new_enrolle');
			}
			else {
				redirect('enroll/'.$this->input->post('enrolle_type'));	
			}
		}
		/*======================End New Enrollee Type Post Actions==================================*/
		
		
		
		/*======================Search Users POST ACTIONS==================================*/
		public function search_user(){
			$this->form_validation->set_rules('username', '', 'alpha_numeric');
			$this->form_validation->set_rules('usertype', '', 'alpha_numeric');
			
			if($this->form_validation->run() == FALSE){
				$this->set_message_redirect('error', validation_errors(), 'admin/search_user');
			}else{
				
				$input['search_user_username'] = $this->input->post('username');
				$input['search_user_usertype'] = $this->input->post('usertype');
				
				if($input['search_user_username'] !== '' OR $input['search_user_usertype'] !== ''){
					$input['search_user'] = 'TRUE';
					$this->session->set_flashdata($input);
					redirect('admin/search_user');
				}else{
					redirect('admin/search_user');
				}
				

			}
		}
		/*======================end Search Users POST ACTIONS==================================*/
		
		/*======================Search Users POST ACTIONS==================================*/
		public function search_student(){
			$this->form_validation->set_rules('firstname', '', 'alpha_numeric');
			$this->form_validation->set_rules('lastname', '', 'alpha_numeric');
			$this->form_validation->set_rules('middlename', '', 'alpha_numeric');
			$this->form_validation->set_rules('id_no', 'Id Number', 'numeric');
			$this->form_validation->set_rules('course_id', 'Course ID','numeric');
			
			
			if($this->form_validation->run() == FALSE){
				$this->set_message_redirect('error', validation_errors(), 'admin/search_student');
			}else{
				
				$input['search_stud_firstname']  = $this->input->post('firstname',TRUE);
				$input['search_stud_lastname'] 	 = $this->input->post('lastname', TRUE);
				$input['search_stud_middlename'] = $this->input->post('middlename', TRUE);
				$input['search_stud_course_id']  = $this->input->post('course_id',TRUE);
				$input['search_stud_id_no'] 	 = $this->input->post('id_no',TRUE);
				
				if($input['search_stud_firstname'] !== '' OR $input['search_stud_lastname'] !== '' OR $input['search_stud_middlename'] OR
				   $input['search_stud_course_id'] !== '' OR $input['search_stud_id_no'] !== ''){
					
					$input['search_stud'] = 'TRUE';
					$this->session->set_flashdata($input);
					redirect('admin/search_student'); 
				}else{
					redirect('admin/search_student'); 
				}
			}
		}
		/*======================end Search Users POST ACTIONS==================================*/
		
		
		/*======================Add COURSE POST ACTIONS==================================*/
		
		public function add_course(){
			$this->load->model('m_course');
			$this->form_validation->set_rules('course_name','','required');
			$this->form_validation->set_rules('course_code','','required');
			$this->form_validation->set_rules('course_unit_value','','required');
			
			if($this->form_validation->run() == FALSE){
				$this->set_message_redirect('error', validation_errors(), 'admin/add_new_course');
			}else{
					$input['course_code'] = $this->input->post('course_code');
					$input['course_name'] = $this->input->post('course_name');
					$input['course_desc'] = $this->input->post('course_desc');
					$input['course_max_unit'] = $this->input->post('course_max_unit');
				if($this->m_course->add_course($input) == FALSE){
					$this->set_message_redirect('error', 'An error was encountered while processing your request', 'admin/add_new_course');
				}else{
					$this->set_message_redirect('success', 'Data has Been Added', 'admin/course');
				}
			}
		}
		
		public function update_course(){
			$this->load->model('m_course');
			$this->form_validation->set_rules('course_name','Course Name','required');
			$this->form_validation->set_rules('course_code','Course Code','required');
			$this->form_validation->set_rules('course_unit_value','Course Unit Value','required');
			$id = $this->input->post('course_id',TRUE);
			$input['course_name'] = $this->input->post('course_name');
			$input['course_desc'] = $this->input->post('course_desc');
			$input['course_code'] = $this->input->post('course_code');
			$input['course_unit_value'] = $this->input->post('course_unit_value');
			if($this->form_validation->run() == FALSE){
				$this->set_message_redirect('error', validation_errors(), 'admin/edit_course/'.$input["id"]);
			}else{
				if($this->m_course->update_course($input,$id) == FALSE){
						$this->set_message_redirect('error', 'An error was encountered while processing your request', 'admin/edit_course/'.$input["id"]);
				}else{
					$this->set_message_redirect('success', 'Data has been updated', 'admin/course');
				}
			}
		}
		
		public function delete_course($id){
			$this->load->model('m_course');
			if($this->m_course->delete_course($id) == FALSE)
				$this->set_message_redirect('error', 'Unable to delete data', 'admin/course/');
			else
				$this->set_message_redirect('success', 'successfully deleted data', 'admin/course/');
			
		}
		/*======================Add COURSE POST ACTIONS end==================================*/
		
		
		/*======================Schoolyear POST FUNCTIONS==================================*/
		public function update_schoolyear(){
			$sy_id				= $this->input->post('sy_id',TRUE);
			$sy_from			= $this->input->post('syfrom',TRUE);
			$sy_now				= $this->input->post('synow',TRUE);
			$input['sy_title']	= $this->input->post('sy_title',TRUE);
			$input['sy_desc']	= $this->input->post('sy_desc',TRUE);
			$input['updated']	= time();
			
			$this->form_validation->set_rules('syfrom','year','required|numeric');
			$this->form_validation->set_rules('synow','year','required|numeric');
			$this->form_validation->set_rules('sy_title','School Year Title','required');
			
			if($this->form_validation->run() == FALSE){
				$this->session->set_flashdata('sy_from',$sy_from);
				$this->session->set_flashdata('sy_now',$sy_now);
				$this->session->set_flashdata('sy_title',$input['sy_title']);
				$this->session->set_flashdata('sy_desc',$input['sy_desc']);
				$this->session->set_flashdata('sy_id',$sy_id);
				$this->set_message_redirect('error', validation_errors(), 'admin/functions_schoolyear/update/'.$sy_id);

			}else{
				$this->load->model('M_school');
				$input['sy_year'] = "$sy_from-$sy_now";
				if($this->M_school->update_school_year($input,$sy_id) == FALSE){
					$this->session->set_flashdata('sy_from',$sy_from);
					$this->session->set_flashdata('sy_now',$sy_now);
					$this->session->set_flashdata('sy_title',$input['sy_title']);
					$this->session->set_flashdata('sy_desc',$input['sy_desc']);
					$this->session->set_flashdata('sy_id',$sy_id);
					$this->set_message_redirect('error', validation_errors(), 'admin/functions_schoolyear/update/'.$sy_id);
				}else{
				
					$this->set_message_redirect('success', 'Changes Has Been Saved', 'admin/school_year');
				}
			
			
			}			
			
			
		}
		
		public function add_schoolyear(){
			$sy_from			= $this->input->post('syfrom',TRUE);
			$sy_now				= $this->input->post('synow',TRUE);
			$input['sy_title']	= $this->input->post('sy_title',TRUE);
			$input['sy_desc']	= $this->input->post('sy_desc',TRUE);
			$input['created']	= time();
			$make_current		= $this->input->post('current',TRUE);
			
			$this->form_validation->set_rules('syfrom','year','required|numeric');
			$this->form_validation->set_rules('synow','year','required|numeric');
			$this->form_validation->set_rules('sy_title','School Year Title','required');
			
			if($this->form_validation->run() == FALSE){
				$this->session->set_flashdata('sy_from',$sy_from);
				$this->session->set_flashdata('sy_now',$sy_now);
				$this->session->set_flashdata('sy_title',$input['sy_title']);
				$this->session->set_flashdata('sy_desc',$input['sy_desc']);
				$this->session->set_flashdata('sy_id',$sy_id);
				$this->set_message_redirect('error', validation_errors(), 'admin/functions_schoolyear/add/');
				
			}else{
				$this->load->model('M_school');
				$input['sy_year'] = "$sy_from-$sy_now";
				if($this->M_school->add_school_year($input,$make_current) == FALSE){
					$this->set_message_redirect('error', 'An error was encountered while processing request', 'admin/functions_schoolyear/add/');
				}else{
					$this->set_message_redirect('success', 'School year saved', 'admin/school_year');
				}
			}
			
			
		}
		/*======================END schoolyear post functions==================================*/
		
		
		/*==========================Misc Functions==============================*/
		private function set_message_redirect($type, $msg, $location){
			$data = array(
				'type' => $type,
				'msg' => $msg
			);
			$this->session->set_flashdata($data);
			redirect($location);
		}
		
		/*==========================Misc Functions==============================*/
	}
?>